//老虎机 start
function tiger(obj, options) { //obj 为被转动 dom 
    if (lucky.isTigerBegin) { return false; }
    lucky.isTigerBegin = 1;

    var aDom = document.querySelectorAll(obj);
    if (!aDom) {
        throw "obj is undefined, can't tiger!!!"
    }
    var len = aDom.length;

    //设置默认选项
    options = options || {};
    options.defaulTurns = options.defaulTurns || 30;// 默认要转的圈数
    options.time = options.time || 5;              // 时间
    options.easing = options.easing || Run.Tween.Quad.easeInOut;// 缓冲
    options.direction = options.direction || "top2bottom";// 方向
    options.initBgPosY = options.initBgPosY || '';     // 初始化位置

    if (!options.height) { // 盒子高度
        throw "height is undefined, can't tiger!!!";
    }
    if (!options.areas_number) { // 奖项数
        throw "areas_number is undefined, can't tiger!!!";
    }

    if (options.defaulTurns < 20) {
        throw "defaulTurns 应该大于等于 20, can't tiger!!!";
    }

    if (!options.initBgPosY) { //初始化位置 不存在
        for (var i = 0; i < len; i++) {
            options.initBgPosY += '0';
        }
    } else if (options.initBgPosY.length != len) {
        throw "initBgPosY应该是" + len + "位的string , can't tiger!!!";
    }

    //设置初始位置
    for (var i = 0; i < len; i++) {
        aDom[i].style['background-position-y'] = - options.height * options.initBgPosY[i] + 'px';
    }


    this.start = function (_target) {

        var finalTarget; //最终中奖目标
        if (_target || _target == 0) {
            finalTarget = _target;
        } else if (options.target || options.target == 0) {
            finalTarget = options.target;
        } else {
            throw "target is undefined, can't tiger!!!";
        }

        if (typeof finalTarget != 'string') {
            throw "target 不是 string, can't tiger!!!";
        }

        if (Number(finalTarget).toString() == "NaN") {
            throw "target不是纯数字的string, can't tiger!!!";
        }

        if (finalTarget.length != len) {
            throw "target应该是" + len + "位的string , can't tiger!!!";
        }

        var randomArr = finalTarget.split("");


        for (var i = 0; i < len; i++) {
            var time = options.time + Math.random() + i; //运动时间随机
            var Y = options.height * options.defaulTurns * options.areas_number - options.height * randomArr[i];
            if (options.direction == "bottom2top") { Y = -Y; }

            Run.to(aDom[i], {
                time: time,
                easing: Run.Tween.Quad.easeInOut,
                backgroundPositionY: Y,
                onUpdate: function (n) {
                    options.onUpdate && options.onUpdate(n);
                },
                onEnd: function () {
                    lucky.isTigerBegin++;

                    for (var j = 0; j < len; j++) {
                        var num = randomArr[j];
                        if (options.direction == "top2bottom") { num = -num; }
                        aDom[j].style['background-position-y'] = num * options.height + 'px';
                     }

                    if ((lucky.isTigerBegin - 1) == len) {
                        options.onEnd && options.onEnd();
                        lucky.isTigerBegin = 0;
                    }
                }
            });
        }

    };

}
//老虎机 end